package com.wuda.lucene;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

import com.wuda.cfg.Configure;
import com.wuda.segmentation.GTSegmenter;
import com.wuda.segmentation.Segmenter;
import com.wuda.segmentation.Token;
import com.wuda.util.CharacterTree;

/**
 * <p>
 * 使用{@link GTSegmenter}分词器
 * </p>
 * 
 * @author wuda
 * 
 */
public class GTTokenFilter extends TokenFilter {

	private CharTermAttribute termAttr = addAttribute(CharTermAttribute.class);
	private OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
	private TypeAttribute typeAtt = addAttribute(TypeAttribute.class);

	private static CharacterTree dic = null;
	private static Segmenter segmenter = null;
	private List<Token> segmentTokens = null;
	private Iterator<Token> tokenIterator = null;

	private int tokStart;
	// private int tokEnd = 0; 暂时不用的属性,以后可能有用

	static {
		dic = new CharacterTree();
		try {
			dic.load(Configure.DIC_RESOURCE); // 加载单词
		} catch (IOException e) {
			e.printStackTrace();
		}
		segmenter = new GTSegmenter(dic);
	}

	protected GTTokenFilter(TokenStream input) {
		super(input);
	}

	@Override
	public boolean incrementToken() throws IOException {
		while ((tokenIterator == null || !tokenIterator.hasNext())
				&& input.incrementToken()) {
			segmentTokens = segmenter.seg(termAttr.toString()); // 获得分词
			if (segmentTokens != null && !segmentTokens.isEmpty()) {
				tokenIterator = segmentTokens.iterator();
				tokStart = offsetAtt.startOffset();
				// tokEnd = offsetAtt.endOffset();
			}
		}
		if (tokenIterator != null && tokenIterator.hasNext()) {
			clearAttributes();
			Token segToken = tokenIterator.next();
			termAttr.copyBuffer(segToken.getCharArray(), 0,
					segToken.getCharArray().length);
			offsetAtt.setOffset(tokStart + segToken.getStartOffset(), tokStart
					+ segToken.getEndOffset());
			typeAtt.setType("word");
			return true;
		}
		return false;
	}

}
